﻿using Microsoft.AspNet.SignalR;
using OASystem.BLL.IService;
using OASystem.BLL.Service;
using OASystem.Common;
using OASystem.Model;
using OASystem.Models;
using OASystem.ViewModel;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace OASystem.Controllers
{
    public class Recode_tail01Controller : Controller
    {
        #region 注入
        private IRecode_tail01Service _recode_tail01Service;
        private ILoginService _loginService;
        public Recode_tail01Controller(IRecode_tail01Service recode_tail01Service, ILoginService loginService)
        {
            _recode_tail01Service = recode_tail01Service;
            _loginService = loginService;
        }
        #endregion

        /// <summary>
        /// 发布单人任务
        /// </summary>
        /// <param name="task">TaskList对象</param>
        /// <param name="Publisher">发布人</param>
        /// <returns></returns>
        public JsonResult Publish_task(Tasklist task, string Publisher)
        {
            ResponseResult result = new ResponseResult();
            result = _recode_tail01Service.Publish_task(task, Publisher);
            if (result.success)
            {
                Tasklist tl = result.data;
                int Id = Convert.ToInt32(tl.Publisher);
                Userinfo userinfo = _loginService.Query(a => a.UserId == Id);
                if (userinfo != null)
                {
                    IHubContext chat = GlobalHost.ConnectionManager.GetHubContext<MyChatHub>();
                    chat.Clients.User(Convert.ToString(tl.oneer)).add("<tr>" +
                        "<td class='contact'>" + tl.TaskTiltle + "</td>" +
                        "<td class='leader'>" + userinfo.EmployeeName + "</td>" +
                        "<td class='contact'>" + tl.ReleaseTime + "</td>" +
                        "</tr>");
                    result.msg = "发布成功!";
                }
                else { result.success = false; result.msg = "失败"; }

            }
            return Json(result);
        }

        /// <summary>
        /// 发布多人任务
        /// </summary>
        /// <param name="task">TaskList对象</param>
        /// <param name="Publisher">发布人</param>
        /// <param name="DepartId">部门ID</param>
        /// <returns></returns>
        public JsonResult Recode_tail01ByMore(Tasklist task, string Publisher, int DepartId)
        {
            //对象id字符串类型集合
            List<string> userId = new List<string>();
            #region 多个通知
            try
            {
                #region 添加成功SingR提交
                ////实例化页面实体层对象
                ResponseResult responseResult = new ResponseResult();
                responseResult = _recode_tail01Service.Recode_tail01ByMore(task, Publisher, DepartId);
                ////接收添加结果是否成功
                List<Tasklist> _tasklist = new List<Tasklist>();
                _tasklist = Recode_tail01Service.TasklistResults;
                for (int i = 0; i < _tasklist.Count; i++)
                {
                    Tasklist tasklist1 = new Tasklist();
                    tasklist1.oneer = _tasklist[i].oneer;
                    userId.Add(tasklist1.oneer.ToString());
                    //singR通知

                    #endregion

                }

                int Id = Convert.ToInt32(_tasklist[0].Publisher);
                Userinfo userinfo = _loginService.Query(a => a.UserId == Id);
                IHubContext chat = GlobalHost.ConnectionManager.GetHubContext<MyChatHub>();
                chat.Clients.Users(userId).add("<tr>" +
                        "<td class='contact'>" + _tasklist[0].TaskTiltle + "</td>" +
                        "<td class='leader'>" + userinfo.EmployeeName + "</td>" +
                        "<td class='contact'>" + _tasklist[0].ReleaseTime + "</td>" +
                        "</tr>");
                responseResult.success = true;
                responseResult.msg = "发布成功!";
                // 提交到前台处理
                return Json(responseResult);
            }
            catch (Exception)
            {

                throw;
            }
            #endregion
        }
        /// <summary>
        /// 显示部门下的所有执行人
        /// </summary>
        /// <param name="departId">所选ID</param>
        /// <returns></returns>
        public JsonResult UserInfoByDepartId(int departId)
        {
            Userinfo userinfo = AdminContext.context.adminUser;
            //接收集合部门表
            List<Userinfo> departinfos = _loginService.QueryList(a => a.DepartId == departId & a.UserId != userinfo.UserId);
            //提交到前台处理
            return Json(departinfos);
        }



    }
}